<?php
include_once('libs/class.database.php');

class Produk
{
	public $link;
	
	function __construct()
	{
		$konek= new Database();
		$this->link=$konek->connect();
	}
	
	function addProduk($nama, $deskripsi, $harga, $satuan, $stok, $gambar ,$id_jns_produk, $id_username)
	{
		$date_created=date('Y-m-d');
		$query=$this->link->prepare("INSERT INTO produk(nama, deskripsi, harga, id_satuan, stok, gambar, id_jns_produk, id_username, date_created) 
			 VALUES(?,?,?,?,?,?,?,?,?)");
		$data=array($nama, $deskripsi, $harga, $satuan, $stok, $gambar, $id_jns_produk, $id_username, $date_created);
		$query->execute($data);
		$jml_row=$query->rowCount();
		return $jml_row;
	}
	
	function getExtension($str) {
		$i = strrpos($str,".");
		if (!$i) { return ""; }
		$l = strlen($str) - $i;
		$ext = substr($str,$i+1,$l);
		return $ext;
	}
	
	function getAllProduk()
	{
		$query=$this->link->query("SELECT * FROM produk LEFT JOIN satuan on produk.id_satuan=satuan.id_satuan ORDER BY date_created ASC");
		$jml_data=$query->rowCount();
		if($jml_data>=1)
		{
			$hasil=$query->fetchAll();
			return $hasil;
		}
		else
		{
			return $jml_data;
		}
	}
	
	function getAllProdukPaging($hal)
	{
		$next=$hal * 5;
		$top= 5;
		if($next==0)
		{
			$query=$this->link->query("SELECT * FROM produk LEFT JOIN satuan on produk.id_satuan=satuan.id_satuan ORDER BY date_created ASC LIMIT $top");
		}
		else
		{
			$query=$this->link->query("SELECT * FROM produk LEFT JOIN satuan on produk.id_satuan=satuan.id_satuan ORDER BY date_created ASC LIMIT $next,$top");
		}
		
		$jml_data=$query->rowCount();
		if($jml_data>=1)
		{
			$hasil=$query->fetchAll();
			return $hasil;
		}
		else
		{
			return $jml_data;
		}
	}
	
	function getHotProduk()
	{
		$query=$this->link->query("SELECT id_produk,SUM(jumlah)  terjual,nama,deskripsi,gambar FROM master_pembelian LEFT JOIN produk ON master_pembelian.id_produk=produk.id GROUP BY id_produk ORDER BY terjual DESC LIMIT 3");
		$jml_data=$query->rowCount();
		if($jml_data>=1)
		{
			$hasil=$query->fetchAll();
			return $hasil;
		}
		else
		{
			return $jml_data;
		}
	}
	
	function getNewProduk()
	{
		$query=$this->link->query("SELECT * FROM produk LEFT JOIN satuan on produk.id_satuan=satuan.id_satuan ORDER BY date_created ASC LIMIT 3");
		$jml_data=$query->rowCount();
		if($jml_data>=1)
		{
			$hasil=$query->fetchAll();
			return $hasil;
		}
		else
		{
			return $jml_data;
		}
	}
	
	function getProduk($id)
	{
		//$query=$this->link->query("SELECT * FROM produk WHERE id='$id' ORDER BY date_created DESC");
		$query=$this->link->query("SELECT c.nama nama,c.id id,c.harga harga,c.deskripsi deskripsi,c.stok stok,c.nama_satuan nama_satuan,c.id_satuan id_satuan,d.nama_jns_sayur nama_jns_sayur, c.id_jns_produk id_jns_produk, c.id_username FROM 
		(SELECT a.`nama`,a.`id`,a.`harga`,a.`deskripsi`,a.`stok`,a.`id_jns_produk`,a.`id_satuan`,b.nama_satuan, a.id_username FROM produk a LEFT JOIN satuan b ON a.`id_satuan`=b.`id_satuan`) c 
		LEFT JOIN jns_sayur d ON c.id_jns_produk=d.`id_jns_sayur` WHERE c.id='$id'");
		$jml_data=$query->rowCount();
		if($jml_data==1)
		{
			$hasil=$query->fetchAll();
			return $hasil;
		}
		else
		{
			return $jml_data;
		}
	}
	
	function getProdukByUsername($id_username)
	{
		$query=$this->link->query("SELECT * FROM produk WHERE id_username='$id_username' ORDER BY date_created DESC");
		$jml_data=$query->rowCount();
		if($jml_data>=1)
		{
			$hasil=$query->fetchAll();
			return $hasil;
		}
		else
		{
			return $jml_data;
		}
	}
	
	function getProdukGerai($id_username)
	{
		$query=$this->link->query("SELECT b.id_beli id_beli,a.id id,a.nama nama,a.stok stok,a.harga harga,SUM(b.jumlah) jumlah FROM produk a,master_pembelian b WHERE a.id_username='$id_username' AND b.id_penjual='$id_username' AND b.id_produk=a.id GROUP BY b.id_penjual,b.id_produk ORDER BY jumlah DESC");
		$jml_data=$query->rowCount();
		if($jml_data>=1)
		{
			$hasil=$query->fetchAll();
			return $hasil;
		}
		else
		{
			return $jml_data;
		}
	}
	
	function getProdukGeraiById($id_username,$id)
	{
		$query=$this->link->query("SELECT c.id_beli id_beli,c.id_pembeli id_pembeli,c.id_produk id_produk,c.harga harga,c.jumlah jumlah,c.tgl_beli tgl_beli,d.nama nama_pembeli,d.email email_pembeli,d.telp telp_pembeli FROM (SELECT a.id_beli id_beli,id_pembeli,id_produk,harga,jumlah,tgl_beli FROM master_pembelian a LEFT JOIN PEMBELIAN b ON a.id_beli=b.id_beli  WHERE id_produk='$id' AND id_penjual='$id_username') c LEFT JOIN biodata d  ON c.id_pembeli=d.id_username");
		$jml_data=$query->rowCount();
		if($jml_data>=1)
		{
			$hasil=$query->fetchAll();
			return $hasil;
		}
		else
		{
			return $jml_data;
		}
	}
	
	function cariProduk($kata)
	{
		$query=$this->link->query("SELECT * FROM produk LEFT JOIN satuan on produk.id_satuan=satuan.id_satuan WHERE nama LIKE '%$kata%' OR deskripsi LIKE '%$kata%' ORDER BY date_created DESC");
		$jml_data=$query->rowCount();
		if($jml_data>=1)
		{
			$hasil=$query->fetchAll();
			return $hasil;
		}
		else
		{
			return $jml_data;
		}
	}
	
	function updateProduk($id, $nama, $deskripsi, $harga, $satuan, $stok )
	{
		$date_updated=date('Y-m-d');
		$query=$this->link->query("UPDATE produk SET nama='$nama', deskripsi='$deskripsi', harga='$harga', id_satuan='$satuan', stok='$stok', date_updated='$date_updated' WHERE id='$id'");
		$hasil=$query->rowCount();
		return $hasil;
	}
	
	function updateProdukWithPic($id, $nama, $deskripsi, $harga, $satuan, $stok, $gambar )
	{
		$date_updated=date('Y-m-d');
		$query=$this->link->query("UPDATE produk SET nama='$nama', deskripsi='$deskripsi', harga='$harga', id_satuan='$satuan', stok='$stok', gambar='$gambar', date_updated='$date_updated' WHERE id='$id'");
		$hasil=$query->rowCount();
		return $hasil;
	}
	
	function updateStok($id,$jml_beli)
	{
		$date_updated=date('Y-m-d');
		$query=$this->link->query("UPDATE produk SET stok=stok-'$jml_beli', date_updated='$date_updated' WHERE id='$id'");
		$hasil=$query->rowCount();
		return $hasil;
	}
}
?>